﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using LD24.Helpers;

namespace LD24.Entities
{
    public class Chunk
    {
        private static Vector3 SIZE = new Vector3(5,5,5);

        private static Vector2 DECALX = new Vector2(16, -8);
        private static Vector2 DECALY = new Vector2(16, 8);
        private static Vector2 DECALZ = new Vector2(0, -16);

        private int[, ,] _blocks;
        private bool[, ,] _visibility;
        private bool _updated;

        public Chunk() : this(Vector3.Zero)
        {
                
        }

        public Chunk(Vector3 position)
        {
            
            this.Position = position;
            _blocks = new int[(int)SIZE.X, (int)SIZE.Y, (int)SIZE.Z];
            for (int x = 0; x < SIZE.X; x++)
                for (int y = 0; y < SIZE.Y; y++)
                    for (int z = 0; z < SIZE.Z; z++)
                        _blocks[x, y, z] = z+1;

            _visibility = new bool[(int)SIZE.X, (int)SIZE.Y, (int)SIZE.Z];
            for (int x = 0; x < SIZE.X; x++)
                for (int y = 0; y < SIZE.Y; y++)
                    for (int z = 0; z < SIZE.Z; z++)
                        _visibility[x, y, z] = true;


            this._updated = true;
        }

        public Vector3 Position { get; set; }

        public void Update(GameTime gameTime)
        {
            if (_updated)
            {
                checkHiddenBlocks();
                _updated = false;
            }

        }

        private void checkHiddenBlocks()
        {
            for (int x = 0; x < SIZE.X; x++)
                for (int y = 0; y < SIZE.Z; y++)
                    for (int z = 0; z < SIZE.Z; z++)
                    {
                        if(x-1 >= 0 && y+1 < SIZE.Y && z+1 < SIZE.Z  )
                        {
                            //if (_blocks[x - 1, y, z] > 0 && _blocks[x + 1, y, z] > 0 && _blocks[x, y - 1, z] > 0 && _blocks[x, y + 1, z] > 0 && _blocks[x, y, z - 1] > 0 && _blocks[x, y, z + 1] > 0)
                            if (_blocks[x - 1, y + 1, z + 1] > 0 ) 
                                _visibility[x, y, z] = false;
                            else
                                _visibility[x, y, z] = true;
                            }
                    }
        }

        private Vector2 getScreenPosition(Vector3 blockPosition)
        {
            Vector2 chunkDecal = Position.X * DECALX * SIZE.X + Position.Y * DECALY * SIZE.Y + Position.Z * DECALZ * SIZE.Z;
            Vector2 blockDecal = blockPosition.X * DECALX + blockPosition.Y * DECALY + blockPosition.Z * DECALZ;

            return chunkDecal + blockDecal;
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            for (int x = (int)SIZE.X-1; x > -1 ; x--)
                for (int y = 0; y < SIZE.Y; y++)
                    //for (int z = (int)SIZE.Z - 1; z > -1; z--)
                    for (int z = 0; z < SIZE.Z; z++)
                        if (_visibility[x, y, z] && _blocks[x, y, z] != 0)
                        {
                            LDGame.DRAW_COUNT++;
                            spriteBatch.Draw(ImageHelper.getImage("block"), getScreenPosition(new Vector3(x, y, z)), ColorHelper.BlockTypeToColor((BlockType)_blocks[x, y, z]));
                        }
        }
    
    
    }
}
